package cn.com.headfree.competent.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.TEmpBaseInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.competent.service.ExportWordUserDataInfoService;
import cn.com.headfree.competent.service.IUserDataInfoService;
import cn.com.headfree.competent.service.TSeniorCompetentHandleService;
import cn.com.headfree.competent.service.WordExportService;
import cn.com.headfree.vo.*;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *职称申报
 */
@Controller
@RequestMapping(value = "/competent", produces = "application/json;charset=UTF-8")
public class CompetentController extends ExBaseController {
	private static final Logger LOG = LogManager.getLogger(CompetentController.class);

	@Autowired
	private IUserDataInfoService userDataInfoService;

	@Autowired
    private TSeniorCompetentHandleService tSeniorCompetentHandleService;

	@Autowired
	private ExportWordUserDataInfoService exportWordUserDataInfoService;

	@Autowired
	private WordExportService wordExportService;

	/**
	 * 基本情况
	 * @param erpNo
	 * @return
	 */
	@RequestMapping("/getTEmpBaseInfoForSenior")
	@ResponseBody
	public ResultData getTEmpBaseInfoForSenior(String erpNo) {
		ResultData rs = new ResultData();
		try {
			TEmpBaseInfo tEmpBaseInfo = userDataInfoService.getBaseInfoByErpNo(erpNo);
			tEmpBaseInfo.setId(null);
            rs.setSuccess(true);
			rs.setData(tEmpBaseInfo);
			LOG.info("查找个人基本情况成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找个人基本情况失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 在岗现状
	 */
	@RequestMapping("/getWorkStatusForSenior")
	@ResponseBody
	public ResultData getWorkStatusForSenior(String erpNo,String seniorId) {
		ResultData rs = new ResultData();
		try {
			List<WorkExpInfoVo> tWorkExpInfos = userDataInfoService.getWorkStatusExpInfoByErpNo(erpNo,seniorId);
            rs.setSuccess(true);
            if(tWorkExpInfos != null && tWorkExpInfos.size() > 0){
				List<WorkExpInfoVo> list = new ArrayList<>();
				list.add(tWorkExpInfos.get(0));
				rs.setData(list);
			}
			LOG.info("查找在岗现状成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找在岗现状失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 学历、学位情况
	 */
	@RequestMapping("/getEducateInfoForSenior")
	@ResponseBody
	public ResultData getEducateInfoForSenior(String erpNo,String seniorId) {
		ResultData rs = new ResultData();
		try {
			List<EducateInfoVo> educateInfos = userDataInfoService.getEducateInfoByErpNo(erpNo,seniorId);
            rs.setSuccess(true);
			rs.setData(educateInfos);
			LOG.info("查找学历、学位情况成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找学历、学位情况失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 学习培训经历
	 */
	@RequestMapping("/getStudyAndTrainForSenior")
	@ResponseBody
	public ResultData getStudyAndTrainForSenior(String erpNo,String seniorId) {
		ResultData rs = new ResultData();
		try {
			List<StudyAndTrainVo> contiStudyInfos = userDataInfoService.getContiStudyInfoByErpNo(erpNo,seniorId);
			rs.setSuccess(true);
			rs.setData(contiStudyInfos);
			LOG.info("查找学习培训经历成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找学习培训经历失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 主要工作经历
	 */
	@RequestMapping("/getTWorkExpInfoForSenior")
	@ResponseBody
	public ResultData getTWorkExpInfoForSenior(String erpNo,String seniorId) {
		ResultData rs = new ResultData();
		try {
			List<WorkExpInfoVo> tWorkExpInfos = userDataInfoService.getWorkExpInfoByErpNo(erpNo,seniorId);
			rs.setSuccess(true);
			rs.setData(tWorkExpInfos);
			LOG.info("查找主要工作经历成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找主要工作经历失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 取得现专业技术资格后的工作成果--科技成果奖励
	 */
	@RequestMapping("/getWorkResultTecManInnovCurrentLater")
	@ResponseBody
	public ResultData getWorkResultTecManInnovCurrentLater(String erpNo, String seniorId, Date acquTime){
		ResultData rs = new ResultData();
		try {
			List<TecManInnovVo> tecManInnovVos = userDataInfoService.getTecManInnovByErpNo(erpNo,seniorId,acquTime);
			rs.setSuccess(true);
			rs.setData(tecManInnovVos);
			LOG.info("查找取得现专业技术资格后科技奖励成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找取得现专业技术资格后科技奖励失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 取得现专业技术资格后的工作成果--荣誉称号
	 */
	@RequestMapping("/getWorkResultHonorInfoCurrentLater")
	@ResponseBody
	public ResultData getWorkResultHonorInfoCurrentLater(String erpNo,String seniorId,Date acquTime){
		ResultData rs = new ResultData();
		try {
			List<HonorDatasVo> honorDatasVos = userDataInfoService.getHonnorInfoByErpNo(erpNo,seniorId,acquTime);
			rs.setSuccess(true);
			rs.setData(honorDatasVos);
			LOG.info("查找取得现专业技术资格后荣誉称号成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找取得现专业技术资格后荣誉称号失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 取得现专业技术资格后的作品成果--学术成果
	 */
	@RequestMapping("/getWorkResultAcadeAchiveCurrentLater")
	@ResponseBody
	public ResultData getWorkResultAcadeAchiveCurrentLater(String erpNo,String seniorId,Date acquTime){
		ResultData rs = new ResultData();
		try {
			List<AcadeAchiveVo> acadeAchiveVos = userDataInfoService.getAcadeAchiveInfoByErpNo(erpNo,seniorId,acquTime);
			if(acadeAchiveVos != null && acadeAchiveVos.size() > 0){
				for(AcadeAchiveVo vo:acadeAchiveVos){
					String pubName = "";
					if("01".equals(vo.getAcadType())){//著作使用出版社名称
						pubName = vo.getPublisher();
					}else{//论文使用刊物名称
						pubName = vo.getPubName();
					}
					vo.setPubName(pubName);
				}
			}
			rs.setSuccess(true);
			rs.setData(acadeAchiveVos);
			LOG.info("查找取得现专业技术资格后学术成果成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找取得现专业技术资格后学术成果失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 取得现专业技术资格后的作品成果--标准制定
	 */
	@RequestMapping("/getWorkResultStandInfoCurrentLater")
	@ResponseBody
	public ResultData getWorkResultStandInfoCurrentLater(String erpNo,String seniorId,Date acquTime){
		ResultData rs = new ResultData();
		try {
			List<StandInfoVo> standInfoVos = userDataInfoService.getStandInfoByErpNo(erpNo,seniorId,acquTime);
			rs.setSuccess(true);
			rs.setData(standInfoVos);
			LOG.info("查找取得现专业技术资格后标准制定成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找取得现专业技术资格后标准制定失败！");
			e.printStackTrace();
		}
		return rs;
	}

    /**
     * 专家称号
     */
    @RequestMapping("/getExpertTitleForSenior")
    @ResponseBody
    public ResultData getExpertTitleForSenior(String erpNo,String seniorId) {
        ResultData rs = new ResultData();
        try {
            List<ExpertTitleVo> expertTitleInfos = userDataInfoService.getExpertTitleInfoByErpNo(erpNo,seniorId);
            rs.setSuccess(true);
            rs.setData(expertTitleInfos);
            LOG.info("查找专家称号成功");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("查找专家称号失败！");
            e.printStackTrace();
        }
        return rs;
    }

	/**
	 * 近三年绩效
	 */
	@RequestMapping("/getThreeYearEffect")
	@ResponseBody
	public ResultData getThreeYearEffect(String erpNo){
		ResultData rs = new ResultData();
		try {
			List<YearEffectVo> yearEffectVos = userDataInfoService.getYearEffectByErpNo(erpNo);
			rs.setSuccess(true);
			rs.setData(yearEffectVos);
			LOG.info("查找近三年绩效成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找近三年绩效失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
     * 操作可见或隐藏
     * @param id
     * @return
     */
    @RequestMapping("/handleHideOrShowForSenior")
    @ResponseBody
    public ResultData handleHideOrShowForSenior(String erpNo,String id,String isHide,String seniorId) {
        ResultData rs = new ResultData();
        try {
            if(StringUtil.isNullOrEmpty(id) || StringUtil.isNullOrEmpty(isHide)){
                rs.setSuccess(false);
                rs.setErrorMsg("参数错误");
                return rs;
            }
			tSeniorCompetentHandleService.solveData(erpNo,id,isHide,seniorId);
            rs.setSuccess(true);
            rs.setData("操作成功");
            LOG.info("操作成功");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("操作失败！");
            e.printStackTrace();
        }
        return rs;
    }


	/**
	 * 职称信息导出到word
	 * @param response
	 */
	@GetMapping("/exportWord")
	public void exportWord(HttpServletRequest request,HttpServletResponse response,String seniorId,Date acquTime){
		User user = this.getLoginUserInfo(request);
		try {
			if (StringUtil.isNullOrEmpty(user.getFax())){
				LOG.error("获取当前登录人人资编号失败");
				return;
			}
			LOG.info("职称申报导出word,查询数据开始");
			//获取待导出的数据
			CompetentReportVo modelData = exportWordUserDataInfoService.getModelData(user.getFax(),seniorId,acquTime);
			LOG.info("信息导出word,查询数据完成,开始导出word");
			//响应页面
			wordExportService.exportWord(response, modelData,user.getName()+"职称申报信息导出",user.getFax());
			LOG.info("个人信息导出word,导出word完成");
		}catch (Exception e){
			e.printStackTrace();
			LOG.error("个人信息导出word异常!!!",e);
		}
	}

}
